package com.chuangke.system.controller;

import com.chuangke.common.http.HttpResult;
import com.chuangke.system.entity.SysConfig;
import com.chuangke.system.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("config")
public class SysConfigController {

	@Autowired
	private SysConfigService sysConfigService;

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/save")
	public HttpResult save(@RequestBody SysConfig sysConfig) {
		sysConfigService.save(sysConfig);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/delete")
	public HttpResult shiftDelete(@RequestBody SysConfig sysConfig) {
		sysConfigService.delete(sysConfig.getId());
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/update")
	public HttpResult update(@RequestBody SysConfig sysConfig) {
		sysConfigService.update(sysConfig);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/list")
	public HttpResult list(@RequestBody Map<String, String> params) {
		String type = params.get("type");
		return HttpResult.ok(sysConfigService.list(type));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/{owner}/{key}")
	public HttpResult find(@PathVariable("owner") String owner, @PathVariable("key") String key) {
		return HttpResult.ok(sysConfigService.find(owner, key));
	}

}
